// pages/location/location.js
const app = getApp()
Page({
  data: {
    _id: '',
    show: false,
    name: '',
    phone: '',
    local: '',
    columns: []
  },
  // 显示选择社区弹窗
  pick () {
    this.setData({
      show: true
    })
  },
  // 输入收货名
  getname (e) {
    this.setData({
      name: e.detail
    })
  },
  // 输入收货手机号码
  getphone(e) {
    this.setData({
      phone: e.detail
    })
  },
  // 确认改变社区
  onConfirm(e) {
    this.setData({
      local: e.detail.value,
      show: false
    })
  },
  // 关闭选择社区弹窗
  onCancel(){
    this.setData({
      show: false
    })
  },
  // 保存地址
  saveAddress(){
    if(!this.data.name||!this.data.phone||!this.data.local){
      wx.showToast({
        title: '请完成信息',
        image: '../../static/icons/err.png',
        duration: 1000
      })
      return
    }
    var token = wx.getStorageSync('token')
    var address = {
      name: this.data.name,
      phone: this.data.phone,
      local: this.data.local
    }
    var model = {
      token: token,
      address:address
    }
    if(wx.getStorageSync('service') == ''){
      wx.setStorageSync('service',this.data.local)
    }
    if(this.data._id){
      var model = {
        token: token,
        address:address,
        _id: this.data._id
      }
      app.api.updateData('admin/address', model, res=>{
        wx.navigateBack({
          delta: 1
        })
      })
    }else{
    app.api.post('admin/address', model, res => {
      wx.navigateBack({
        delta: 1
      })
    })
    }
  },
  onLoad: function (options) {
    var _id = options.id
    this.setData({
      _id: _id
    })
   
    let a = {address:app.globalData.address}
    app.api.getData('admin/customer',a,(res)=>{
      this.setData({
        columns:res.data
      })
    })
    if(_id){
    var data = app.globalData.addressList.filter(e=>{
      return e._id == _id
    })
    this.setData({
      name: data[0].name,
      phone: data[0].phone,
      local: data[0].local
    })
    }
  },
  onShow:function(){
    wx.showToast({
      title: '请选择社区服务点',
      icon: 'none',    //如果要纯文本，不要icon，将值设为'none'
      duration: 2000
    })  
  }
})